package com.jieli.stream.dv.running2.ui;

import android.app.Application;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.text.TextUtils;

import com.baidu.mapapi.SDKInitializer;
import com.jieli.lib.dv.control.DeviceClient;
import com.jieli.stream.dv.running2.bean.DeviceDesc;
import com.jieli.stream.dv.running2.bean.DeviceSettingInfo;
import com.jieli.stream.dv.running2.util.AppUtils;
import com.jieli.stream.dv.running2.util.ClientManager;
import com.jieli.stream.dv.running2.util.Dbug;
import com.jieli.stream.dv.running2.util.HandlerUtil;
import com.jieli.stream.dv.running2.util.PreferencesHelper;
import com.jieli.stream.dv.running2.util.ToastUtil;
import com.jieli.stream.dv.running2.util.WifiHelper;

import static com.jieli.stream.dv.running2.util.IConstant.DIR_FRONT;
import static com.jieli.stream.dv.running2.util.IConstant.DIR_REAR;
import static com.jieli.stream.dv.running2.util.IConstant.KEY_APP_LANGUAGE_CODE;
import static com.jieli.stream.dv.running2.util.IConstant.KEY_ROOT_PATH_NAME;

/**
 * Application类
 * date : 2017-02-27
 */
public class MainApplication extends Application {
    private static MainApplication sMyApplication = null;
    private DeviceDesc deviceDesc;

    private int appVersion;
    private int searchMode;
    private String appName;
    private String appVersionName;
    private String UUID;
    private boolean sdcardExist;
    private boolean isUpgrading;

    private DeviceSettingInfo deviceSettingInfo;
    public static boolean isFactoryMode = false;
    private boolean isWifiDirectGO = false;

    @Override
    public void onCreate() {
        super.onCreate();
        sMyApplication = this;
        ToastUtil.init(this);
        SDKInitializer.initialize(sMyApplication);
        appName = PreferencesHelper.getSharedPreferences(getApplicationContext()).getString(KEY_ROOT_PATH_NAME, null);
        PackageManager pm = this.getPackageManager();
        if (TextUtils.isEmpty(appName)) {
            appName = getApplicationInfo().loadLabel(pm).toString();
        }
        try {
            appVersion = pm.getPackageInfo(getPackageName(), 0).versionCode;
            appVersionName = pm.getPackageInfo(getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        deviceDesc = new DeviceDesc();
        deviceSettingInfo = new DeviceSettingInfo();
//        LangUtil.changeLanguage(getApplication());
    }

    public static synchronized MainApplication getApplication() {
        return sMyApplication;
    }

    public String getUUID() {
        return UUID;
    }

    public void setUUID(String UUID) {
        this.UUID = UUID;
    }

    public boolean isSdcardExist() {
        return sdcardExist;
    }

    public void setSdcardExist(boolean sdcardExist) {
        this.sdcardExist = sdcardExist;
    }

    public DeviceSettingInfo getDeviceSettingInfo() {
        return deviceSettingInfo;
    }

    public String getAppName() {
        return appName;
    }

    public int getAppVersion() {
        return appVersion;
    }

    public String getAppVersionName() {
        return appVersionName;
    }

    public void setDeviceDesc(DeviceDesc deviceDesc) {
        if(deviceDesc != null){
            this.deviceDesc = deviceDesc;
        }
    }

    public DeviceDesc getDeviceDesc() {
        return deviceDesc;
    }

    public boolean isUpgrading() {
        return isUpgrading;
    }

    public void setUpgrading(boolean upgrading) {
        isUpgrading = upgrading;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getCameraDir(){
        String dir;
        if(deviceSettingInfo.getCameraType() == DeviceClient.CAMERA_REAR_VIEW){
            dir = DIR_REAR;
        }else {
            dir = DIR_FRONT;
        }
        return dir;
    }

    public int getSearchMode() {
        return searchMode;
    }

    public void setSearchMode(int searchMode) {
        this.searchMode = searchMode;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Dbug.w(getClass().getSimpleName(), "onConfigurationChanged orientation : " + newConfig.orientation);
        String index = PreferencesHelper.getSharedPreferences(getApplication()).getString(KEY_APP_LANGUAGE_CODE, "-1");
        //系统配置改变时需要重新设置语言
        AppUtils.changeAppLanguage(getApplication(), index);
    }

    public void switchWifi() {
        ClientManager.getClient().close();
        HandlerUtil.postDelayed(new Runnable() {
            @Override
            public void run() {
                WifiHelper.getInstance(sMyApplication).removeCurrentNetwork(sMyApplication);
            }
        }, 1000);
    }

    public boolean isWifiDirectGO() {
        return isWifiDirectGO;
    }
}
